package com.ruoyi.procurementmanagement.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.procurementmanagement.domain.ErpWarehouseEntry;
import com.ruoyi.procurementmanagement.service.IErpWarehouseEntryService;
import com.ruoyi.procurementmanagement.service.IEsProcurementContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurementmanagement.mapper.ErpSupplierReturnMapper;
import com.ruoyi.procurementmanagement.domain.ErpSupplierReturn;
import com.ruoyi.procurementmanagement.service.IErpSupplierReturnService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商退货Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@Slf4j
@Service
public class ErpSupplierReturnServiceImpl implements IErpSupplierReturnService
{
    @Autowired
    private ErpSupplierReturnMapper erpSupplierReturnMapper;

    @Autowired
    private IErpWarehouseEntryService warehouseEntryService;

    @Autowired
    private IEsProcurementContractService contractService;

    /**
     * 查询供应商退货
     *
     * @param id 供应商退货主键
     * @return 供应商退货
     */
    @Override
    public ErpSupplierReturn selectErpSupplierReturnById(Long id)
    {
        return erpSupplierReturnMapper.selectErpSupplierReturnById(id);
    }

    /**
     * 查询供应商退货列表
     *
     * @param erpSupplierReturn 供应商退货
     * @return 供应商退货
     */
    @Override
    public List<ErpSupplierReturn> selectErpSupplierReturnList(ErpSupplierReturn erpSupplierReturn)
    {
        return erpSupplierReturnMapper.selectErpSupplierReturnList(erpSupplierReturn);
    }

    /**
     * 新增供应商退货
     *
     * @param erpSupplierReturn 供应商退货
     * @return 结果
     */
    @Override
    public int insertErpSupplierReturn(ErpSupplierReturn erpSupplierReturn)
    {
        erpSupplierReturn.setCreateTime(DateUtils.getNowDate());
        return erpSupplierReturnMapper.insertErpSupplierReturn(erpSupplierReturn);
    }

    /**
     * 修改供应商退货
     *
     * @param erpSupplierReturn 供应商退货
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateErpSupplierReturn(ErpSupplierReturn erpSupplierReturn) {
        try {
            // 1. 更新退货单
            int result = erpSupplierReturnMapper.updateErpSupplierReturn(erpSupplierReturn);

            // 2. 状态联动
            if ("审核通过".equals(erpSupplierReturn.getStatus())) {
                log.info("开始处理状态联动...");

                // 入库单状态更新
                if (erpSupplierReturn.getWarehouseEntryId() != null) {
                    ErpWarehouseEntry entry = warehouseEntryService.selectErpWarehouseEntryById(
                            erpSupplierReturn.getWarehouseEntryId());

                    if (entry != null && !"已退货".equals(entry.getStatus())) {
                        entry.setStatus("已退货");
                        warehouseEntryService.updateErpWarehouseEntry(entry);
                        log.info("入库单状态已更新为已退货");
                    }
                }

                // 合同状态更新
                if (erpSupplierReturn.getContractId() != null) {
                    contractService.updateContractStatus(
                            erpSupplierReturn.getContractId().intValue(),
                            "已关闭");
                    log.info("合同状态已更新为已关闭");
                }
            }
            return result;
        } catch (Exception e) {
            log.error("更新退货单失败：", e);
            throw new ServiceException("更新退货单失败：" + e.getMessage());
        }
    }


    /**
     * 批量删除供应商退货
     *
     * @param ids 需要删除的供应商退货主键
     * @return 结果
     */
    @Override
    public int deleteErpSupplierReturnByIds(Long[] ids)
    {
        return erpSupplierReturnMapper.deleteErpSupplierReturnByIds(ids);
    }

    /**
     * 删除供应商退货信息
     *
     * @param id 供应商退货主键
     * @return 结果
     */
    @Override
    public int deleteErpSupplierReturnById(Long id)
    {
        return erpSupplierReturnMapper.deleteErpSupplierReturnById(id);
    }
}
